package com.simtop.controller;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.simtop.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.service.XqyxModelThreeService;
import com.simtop.util.ResultBean;
import com.simtop.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/sqyxcp")
//@CrossOrigin //todo 跨域问题
public class XqyxModelThree {



    @Autowired
    private XqyxModelThreeService xqyxModelThreeService;

    @RequestMapping("/gitOnelb")
    @ResponseBody
    public ResultBean gitOnelb() {
        List<String> gitOnelb = xqyxModelThreeService.gitOnelb();

        return ResultBean.success(gitOnelb);
    }

    @RequestMapping("/gitOnebyid")
    @ResponseBody
    public ResultBean gitOnebyid(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String cplb = jsonObject.getString("cplb");
        List<String> gitOnebyids = xqyxModelThreeService.gitOnebyid(cplb);
        return ResultBean.success(gitOnebyids);
    }

    @RequestMapping("/gitAllcp")
    @ResponseBody
    public ResultBean gitAllcp(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String one = jsonObject.getString("one");
        String two = jsonObject.getString("two");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");

        //初始化页面信息
        if (StringUtils.isNull(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        //启动分页
        PageHelper.startPage(pageNum, pageSize);
        //显示所有用户
        List<SqyxCp> gitAllcp = xqyxModelThreeService.gitAllcp(one, two);
        PageInfo<SqyxCp> pageInfo = new PageInfo<SqyxCp>(gitAllcp);
        return ResultBean.success("获取社群用户信息成功", pageInfo);
    }


    @RequestMapping("/gitjqcp")
    @ResponseBody
    public ResultBean gitjqcp(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String onecp = jsonObject.getString("onecp");
        String twocp = jsonObject.getString("twocp");
        List<String> gitjqcp = xqyxModelThreeService.gitjqcp(onecp, twocp);
        List<Object> objects = new ArrayList<Object>();
        for (String sq : gitjqcp
        ) {
            String[] s = sq.split(" ");
            objects.add(s[0]);
        }
        //去重
        LinkedHashSet<Object> hashSet = new LinkedHashSet<>(objects);
        ArrayList<Object> listWithoutDuplicates = new ArrayList<>(hashSet);
        return ResultBean.success(listWithoutDuplicates);
    }


    @RequestMapping("/gitOnecp")
    @ResponseBody
    public ResultBean gitOnecp(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String one = jsonObject.getString("one");
        String two = jsonObject.getString("two");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");

        //初始化页面信息
        if (StringUtils.isNull(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        //启动分页
        PageHelper.startPage(pageNum, pageSize);
        //显示所有用户
        List<SqyxCp> gitOnecp = xqyxModelThreeService.gitOnecp(one, two);
        PageInfo<SqyxCp> pageInfo = new PageInfo<SqyxCp>(gitOnecp);
        return ResultBean.success("获取社群用户信息成功", pageInfo);
    }

    @RequestMapping("/showcp")
    @ResponseBody
    public ResultBean showcp(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String onecp = jsonObject.getString("onecp");
        String twocp = jsonObject.getString("twocp");
        String threecp = jsonObject.getString("threecp");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");

        //初始化页面信息
        if (StringUtils.isNull(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize) || pageSize == 0) {
            pageSize = 10;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<SqyxCp> showcp = xqyxModelThreeService.showcp(onecp, twocp, threecp);
        PageInfo<SqyxCp> pageInfo = new PageInfo<SqyxCp>(showcp);
        return ResultBean.success("获取社群用户信息成功", pageInfo);
    }

    @RequestMapping("/showxzcp")
    @ResponseBody
    public ResultBean showxzcp(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String ids =  jsonObject.getString("ids");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        List<String> result = Arrays.asList(ids.split(","));
        ArrayList<Integer> idss = new ArrayList<>();
        for (String as:result
        ) {
            idss.add(Integer.valueOf(as));

        }



        //初始化页面信息
        if (StringUtils.isNull(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        //启动分页
        PageHelper.startPage(pageNum, pageSize);
        //显示所有用户
        List<SqyxCp> gitAllxzcps =  xqyxModelThreeService.gitAllxzcps(idss);
        PageInfo<SqyxCp> pageInfo = new PageInfo<SqyxCp>(gitAllxzcps);
        return ResultBean.success("获取社群用户信息成功",pageInfo);

    }

    @RequestMapping("/cpcy")
    @ResponseBody
    public ResultBean cpcy(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        //业务增长率
        String zz = jsonObject.getString("zz");
        //市场占有率
        String zy = jsonObject.getString("zy");


        //如果是低 小于 250  大于  50000
        if(zz.equals("低") && zy.equals("高")){
            List<NewCpVo> cpcyone = xqyxModelThreeService.cpcyone();
            return ResultBean.success(cpcyone);
        }
        //如果是低 小于 250  小于  50000

        if(zz.equals("低") && zy.equals("低")){
            List<NewCpVo> cpcytwo = xqyxModelThreeService.cpcytwo();
            return ResultBean.success(cpcytwo);

        }
        //如果是低 高于 250  低于  50000
        if(zz.equals("高") && zy.equals("低")){
            List<NewCpVo> cpcythree = xqyxModelThreeService.cpcythree();
            return ResultBean.success(cpcythree);
        }
        //如果是 高于 250  高于  50000
        if(zz.equals("高") && zy.equals("高")){
            List<NewCpVo> cpcyfor = xqyxModelThreeService.cpcyfor();
            return ResultBean.success(cpcyfor);
        }




        List<NewCpVo> cpcy = xqyxModelThreeService.cpcy();



        return ResultBean.success(cpcy);


    }

    @RequestMapping("/cpcysg")
    @ResponseBody
    public ResultBean cpcysg() {
        //低 小于 250  大于  50000
        List<NewCpVo> cpcyones = xqyxModelThreeService.cpcyones();



        return  ResultBean.success(cpcyones);

    }

    @RequestMapping("/gitOnelbxz")
    @ResponseBody
    public  ResultBean gitOnelbxz(){
        List<String> gitOnelbxz = xqyxModelThreeService.gitOnelbxz();
        ArrayList<Object> objects = new ArrayList<>();

        for (String s:gitOnelbxz
             ) {
            if (s.equals("")){
                s = "其他";

            }
            System.out.println(s);
            objects.add(s);

        }

        return ResultBean.success(objects);

    }


    @RequestMapping("/gitTwolbxz")
    @ResponseBody
    public ResultBean gitTwolbxz(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        ArrayList<Object> objects = new ArrayList<>();
        String cplb = jsonObject.getString("cplb");
        if (cplb.equals("其他")){
            cplb = "";
        }

        List<String> gitTwolbxz = xqyxModelThreeService.gitTwolbxz(cplb);

        for (String gigi:gitTwolbxz
             ) {
            if(gigi.equals("")){
                gigi = "其他";
            }
            objects.add(gigi);
        }

        return ResultBean.success(objects);
    }

    @RequestMapping("/gitAllxzcp")
    @ResponseBody
    public ResultBean gitAllxzcp(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String one = jsonObject.getString("one");
        String two = jsonObject.getString("two");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        //初始化页面信息
        if (StringUtils.isNull(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        //启动分页
        PageHelper.startPage(pageNum, pageSize);
        //显示所有用户
        List<SqyxXzcp> gitAllxzcp =  xqyxModelThreeService.gitAllxzcp(one,two);
        PageInfo<SqyxXzcp> pageInfo = new PageInfo<SqyxXzcp>(gitAllxzcp);
        return ResultBean.success("获取社群用户信息成功",pageInfo);
    }



    @RequestMapping("/cpxj")
    @ResponseBody
    public ResultBean cpxj(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String cpname = jsonObject.getString("cpname");
        //将所有namestatus 设置为1
        xqyxModelThreeService.cpxj(cpname);
        return ResultBean.success("下架成功");

    }

    @RequestMapping("/zssj")
    @ResponseBody
    public ResultBean zssj(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String onecp = jsonObject.getString("onecp");
        String twocp = jsonObject.getString("twocp");
        String threecp = jsonObject.getString("threecp");

        List<SqyxCp> showcp = xqyxModelThreeService.showcptwo(onecp, twocp, threecp);
        //获取开始结束日期

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SqyxCp qw = showcp.get(0);
        Date cptimebegin = qw.getCptime();
        String first = sdf.format(cptimebegin);
        System.out.println(first);
        SqyxCp qwe = showcp.get(showcp.size()-1);
        Date cptimelast = qwe.getCptime();
        String last = sdf.format(cptimelast);
        System.out.println(last);


        //获取总销售额
        int findallxs = xqyxModelThreeService.findallxs(first,last);
        //获取到当前销售额
        int a = 0;
        for (SqyxCp ss:showcp
             ) {
          a =a + ss.getCpjg();
        }
        //查询所有产品
        int findallcp = xqyxModelThreeService.findallcp(first,last);
        //剩下的销售量
        int i = findallcp - showcp.size();
        //该产品销量

        int uu = findallxs - a;

        List<DateTime> dateTimes = DateUtil.rangeToList(cptimebegin, cptimelast, DateField.DAY_OF_YEAR);
        ArrayList<Object> objects = new ArrayList<>();
        ArrayList<Object> objects2 = new ArrayList<>();
        ArrayList<Object> objects3 = new ArrayList<>();
        ArrayList<Object> objects4 = new ArrayList<>();
        ArrayList<Object> objects5 = new ArrayList<>();
        for (DateTime da : dateTimes
        ) {

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            String everday = sdf2.format(da);

            //获取到当前产品 每天效率额
            int   dqcpxs=xqyxModelThreeService.dqcpxs(onecp, twocp, threecp,everday);
            //总体销量
            int   dqcpxss=xqyxModelThreeService.dqcpxss(everday);

            //获取到当前产品 每天销量
            int   adqcpxs=xqyxModelThreeService.adqcpxs(onecp, twocp, threecp,everday);

            //获取所有产品销量
            int allcpcpcp = xqyxModelThreeService.adqcpxss(everday);

            //找到当前产品的每一天



            objects3.add(dqcpxss);
            System.out.println(everday);
            objects2.add(dqcpxs);
            objects.add(everday);
            objects4.add(adqcpxs);
            objects5.add(allcpcpcp);

        }




        Xltj xltj = new Xltj();
        xltj.setXldate(objects);
        xltj.setCpcpsl(objects2);
        xltj.setAllcpcpsl(objects3);

        Xstj xstj = new Xstj();
        xstj.setXsdate(objects);
        xstj.setCpcpxs(objects4);
        xstj.setAllcpcpxs(objects5);



        LastModel lastModel = new LastModel();
        lastModel.setXl(i);
        lastModel.setCpxl(showcp.size());
        lastModel.setZxs(uu);
        lastModel.setCpxs(findallcp);
        lastModel.setXltj(xltj);
        lastModel.setXstj(xstj);



        return ResultBean.success("展示成功",lastModel);
    }


    //拿到新增数据 加入 总数据
    @RequestMapping("/xj")
    @ResponseBody
    public ResultBean xj(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        int cpid = jsonObject.getInteger("cpid");
        String cpgm = jsonObject.getString("cpgm");
        String cptitle = jsonObject.getString("cptitle");
        int cpjg = jsonObject.getInteger("cpjg");
        String cplb = jsonObject.getString("cplb");
        int cpstatus = jsonObject.getInteger("cpstatus");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());

        //先去删除id重复的
        xqyxModelThreeService.delectxj(cpid);

        xqyxModelThreeService.xj(cpid,cpgm,cptitle,cpjg,cplb,cpstatus,format);

        return ResultBean.success("新增成功");
    }


    @RequestMapping("/delectbycpid")
    @ResponseBody
    public ResultBean delectbycpid(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        int cpid = jsonObject.getInteger("cpid");
        xqyxModelThreeService.delectbycpid(cpid);

        return ResultBean.success("修改成功");
    }



    }
